import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:playtruly/pages/play/activte/view.dart';
import 'package:playtruly/pages/play/my_publish/view.dart';
import 'package:playtruly/pages/play/review/view.dart';

import 'index.dart';

class PlayIndexPage extends GetView<PlayIndexController> {
  const PlayIndexPage({super.key});

  // 构建悬浮的TabBar
  Widget _buildFloatingTabBar(BuildContext context) {
    // 根据当前选中的Tab判断是否是“我发布的”页面
    final bool isMyPublishTab = controller.state.currentTabIndex == 2;
    // 根据页面背景动态设置Tab文字颜色
    final Color labelColor = isMyPublishTab ? Colors.white : Colors.black;

    return Positioned(
      top: 0,
      left: 0,
      right: 0,
      child: Container(
        color: Colors.transparent,
        padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
        child: Container(
          // TabBar的背景始终保持透明，以透出下方页面的内容
          color: Colors.transparent,
          height: kToolbarHeight - 10,
          margin: EdgeInsets.symmetric(
              horizontal: MediaQuery.of(context).size.width * 0.1),
          child: TabBar(
            controller: controller.tabController,
            // 动态设置标签文字颜色
            labelColor: labelColor,
            unselectedLabelColor: labelColor,
            indicatorColor: Colors.red,
            indicatorSize: TabBarIndicatorSize.label,
            labelPadding: const EdgeInsets.symmetric(horizontal: 0),
            dividerColor: Colors.transparent,
            indicatorPadding: const EdgeInsets.symmetric(horizontal: 0),
            splashFactory: NoSplash.splashFactory,
            overlayColor: MaterialStateProperty.all(Colors.transparent),
            indicator: const BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Colors.red,
                  width: 2.0,
                ),
              ),
            ),
            labelStyle: const TextStyle(
              fontSize: 16.0,
              fontWeight: FontWeight.bold,
            ),
            unselectedLabelStyle: const TextStyle(
              fontSize: 16.0,
            ),
            tabs: const [
              Tab(child: Center(child: Text('评论'))),
              Tab(child: Center(child: Text('活动'))),
              Tab(child: Center(child: Text('我发布的'))),
            ],
          ),
        ),
      ),
    );
  }

  // 构建TabBarView
  Widget _buildTabBarView(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
          top: MediaQuery.of(context).padding.top + kToolbarHeight - 10),
      child: TabBarView(
        // 设置 physics 禁止滑动切换
        physics: const NeverScrollableScrollPhysics(),
        controller: controller.tabController,
        children: const [
          ReviewPageContent(),
          ActivtePageContent(),
          MyPublishPageContent(),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlayIndexController>(
      builder: (_) {
        return Scaffold(
          // 移除 extendBodyBehindAppBar 和 appBar，以解决手势冲突
          body: Stack(
            children: [
              _buildTabBarView(context),
              _buildFloatingTabBar(context),
            ],
          ),
        );
      },
    );
  }
}

// 评论页面内容
class ReviewPageContent extends StatelessWidget {
  const ReviewPageContent({super.key});

  @override
  Widget build(BuildContext context) {
    return const ReviewPage();
  }
}

// 活动页面内容
class ActivtePageContent extends StatelessWidget {
  const ActivtePageContent({super.key});

  @override
  Widget build(BuildContext context) {
    return const ActivtePage();
  }
}

// 我发布的页面内容
class MyPublishPageContent extends StatelessWidget {
  const MyPublishPageContent({super.key});

  @override
  Widget build(BuildContext context) {
    return const MyPublishPage();
  }
}
